package com.study.algorithm.search;

import java.util.ArrayDeque;
import java.util.Queue;

public class BFS {


    public static void bfs(BinTree tree) {
        if (tree == null) {
            return;
        }
        Queue<BinTree> queue = new ArrayDeque<>();
        queue.add(tree);
        while (!queue.isEmpty()) {
            BinTree poll = queue.poll();
            if (poll == null) {
                continue;
            }
            System.out.println("poll = " + poll.getData());
            if (poll.getLeft() != null) {
                queue.add(poll.getLeft());
            }
            if (poll.getRight() != null) {
                queue.add(poll.getRight());
            }
        }
    }

    public static void main(String[] args) {
        /**
         *      1
         *   2     3
         * 4  5   6  7
         */
        BinTree tree4 = new BinTree(4);
        BinTree tree5 = new BinTree(5);
        BinTree tree6 = new BinTree(6);
        BinTree tree7 = new BinTree(7);
        BinTree tree3 = new BinTree(3, tree6, tree7);
        BinTree tree2 = new BinTree(2, tree4, tree5);
        BinTree tree1 = new BinTree(1, tree2, tree3);
        bfs(tree1);
    }
}
